package org.greatstorm.service;

import org.greatstorm.dao.AlbumDao;
import org.greatstorm.entity.Album;
import org.greatstorm.entity.EntityObject;
import org.greatstorm.exception.KeyRepeatException;
import org.greatstorm.tx.Transaction;
import org.greatstorm.tx.TransactionManager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class AlbumManager {
	@Inject
	AlbumDao albDao;

	public void addAlbum(String name, int visible) {
		Transaction tx = TransactionManager.getTransaction();
		tx.beginTransaction();

		// check whether the adding album exists
		EntityObject ret = albDao.getAlbumByName(name);

		if (ret != null) {
			// exist
			throw new KeyRepeatException("album name has existed!");
		}

		EntityObject album = new EntityObject();
		album.put(Album.ALBUM_NAME, name);
		album.put(Album.VISIBLE, visible);
		album.put(Album.PHOTOCOUNT, 0);
		albDao.save(album);

		tx.commit();
	}

	public void deleteAll() {
		albDao.deleteAll();
	}
}
